import { getDictDataByType } from '@/api/dict'
import { ref, onMounted } from 'vue'

/**
 * 字典数据缓存
 */
const dictCache = {}

/**
 * 设置字典缓存
 * @param {string} dictType 字典类型
 * @param {Array} dictData 字典数据
 */
export function setDictCache(dictType, dictData) {
  dictCache[dictType] = dictData
}

/**
 * 获取字典缓存
 * @param {string} dictType 字典类型
 * @returns {Array|null} 字典数据
 */
export function getDictCache(dictType) {
  return dictCache[dictType] || null
}

/**
 * 清除字典缓存
 * @param {string} [dictType] 字典类型，不传则清除所有
 */
export function clearDictCache(dictType) {
  if (dictType) {
    delete dictCache[dictType]
  } else {
    Object.keys(dictCache).forEach(key => {
      delete dictCache[key]
    })
  }
}

/**
 * 获取字典数据
 * @param {string} dictType 字典类型
 * @returns {Promise<Array>} 字典数据
 */
export async function getDictData(dictType) {
  // 先从缓存获取
  const cached = getDictCache(dictType)
  if (cached) {
    return cached
  }
  
  // 缓存不存在，从后端获取
  try {
    const res = await getDictDataByType(dictType)
    const dictData = res.data || []
    
    // 设置缓存
    setDictCache(dictType, dictData)
    
    return dictData
  } catch (error) {
    console.error(`获取字典数据[${dictType}]失败:`, error)
    return []
  }
}

/**
 * 获取字典标签
 * @param {string} dictType 字典类型
 * @param {string|number} value 字典值
 * @param {string} [defaultLabel] 默认标签
 * @returns {string} 字典标签
 */
export function getDictLabel(dictType, value, defaultLabel = '') {
  const cached = getDictCache(dictType)
  if (!cached) {
    return defaultLabel
  }
  
  const dictItem = cached.find(item => item.value === value)
  return dictItem ? dictItem.label : defaultLabel
}

/**
 * 获取字典标签对应的样式类
 * @param {string} dictType 字典类型
 * @param {string|number} value 字典值
 * @returns {string} 样式类
 */
export function getDictClass(dictType, value) {
  const cached = getDictCache(dictType)
  if (!cached) {
    return ''
  }
  
  const dictItem = cached.find(item => item.value === value)
  return dictItem ? dictItem.listClass : ''
}

/**
 * 加载多个字典数据
 * @param {Array<string>} dictTypes 字典类型数组
 * @returns {Promise<Object>} 包含所有请求字典的对象
 */
export async function loadDictData(dictTypes) {
  const result = {}
  
  const tasks = dictTypes.map(async (dictType) => {
    const dictData = await getDictData(dictType)
    result[dictType] = dictData
  })
  
  await Promise.all(tasks)
  
  return result
}

/**
 * 字典项组件选项
 * @param {string} dictType 字典类型
 * @returns {Array} 字典选项数组
 */
export async function getDictOptions(dictType) {
  const dictData = await getDictData(dictType)
  
  // 只返回正常状态的选项
  return dictData
    .filter(item => item.status === 1)
    .map(item => ({
      label: item.label,
      value: item.value,
      type: item.listClass || ''
    }))
}

/**
 * 字典项下拉组件
 * @param {string} dictType 字典类型
 */
export function useDictOptions(dictType) {
  const options = ref([])
  const loading = ref(false)
  
  onMounted(async () => {
    loading.value = true
    try {
      options.value = await getDictOptions(dictType)
    } finally {
      loading.value = false
    }
  })
  
  return {
    options,
    loading
  }
} 